// Package middlewares
// Created by GoLand
// @User: lenora
// @Date: 2023/12/2
// @Time: 16:32

package middlewares

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"net/http"
)

const SessionKey = "userId"

type MiddlewareBuilder struct {
}

func (m *MiddlewareBuilder) CheckLogin() gin.HandlerFunc {
	return func(context *gin.Context) {
		path := context.Request.URL.Path
		if path == "/users/signup" || path == "/users/login" {
			return
		}
		sess := sessions.Default(context)
		if sess.Get(SessionKey) == nil {
			//未登录,停止执行后续业务逻辑
			context.AbortWithStatus(http.StatusUnauthorized)
			return
		} else {
			//fmt.Printf("%+v\n", sess.Get("userId"))
		}
	}
}
